﻿using ControllerClient;
using NewCash2DeviceServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ControlMachine
{
    class EpcScan
    {
       
        static LogHelperNew ltKey = new LogHelperNew("processKey");
        public static byte[] DataInstruct(byte[] processKey, List<byte> epc)
        {
            List<byte> data = new List<byte>();
            data.Add(0x20);
           
            data.Add(0x05);

            byte[] epcData = epc.ToArray();
           
            byte[] dataByte = HexUtils.intToBytes(epcData.Length);
            for (int i = 0; i < dataByte.Length; i++)
            {
                data.Add(dataByte[i]);

            }
            for (int i = 0; i < epcData.Length; i++)
            {
                data.Add( epcData[i]);

            }

            Console.WriteLine("data.ToArray()" + HexUtils.byteToHexStr(data.ToArray()));
          
            byte[] macByte = KCRXMAC.GetMACTest(data.ToArray(), processKey);
            Console.WriteLine("macByte" + HexUtils.byteToHexStr(macByte));
            for (int i = 0; i < macByte.Length; i++)
            {
                data.Add(macByte[i]);
            }
           
            return data.ToArray();

        }

        public static byte[] EpcReponse(byte[] processKey)
        {
            byte[] data = new byte[10];
            data[0]= 0x25;
            data[1] = 0x95;
            byte[] dataByte = HexUtils.intToBytes(0);
            for (int i = 0; i < dataByte.Length; i++)
            {
                data[2 + i] = dataByte[i];

            }
            byte[] dataIntercept = new byte[6];
            Array.Copy(data, 0, dataIntercept, 0, 6);
            ulong macReal = KCRXMAC.GetMAC(dataIntercept, processKey);
            byte[] macByte = HexUtils.strToHexByte(HexUtils.byteToHexStr(BitConverter.GetBytes(macReal)).Substring(0, 8));

            Array.Copy(macByte, 0, data, 6, 4);
            return data;

        }

        public static byte[] test(string data,int dataLen)
        {
           
            byte[] processKey = HexUtils.strToHexByte("1111111111111111");
            List<byte> epc = new List<byte>();
            //byte[] epc = new byte[476] ;
           // log.WriteLine("len"+ data.Length / 14);
            byte[] len = HexUtils.intToBytes(dataLen);
            for (int i = 0; i < len.Length; i++)
            {
                epc.Add(len[i]);
            }
            //data = "1210000002300112100000033001121000000430011210000005300112100000063001";
            byte[] epcdata =Encoding.UTF8.GetBytes(data);
            for (int i = 0; i < epcdata.Length; i++)
            {
                epc.Add(epcdata[i]);
            }
            string str = HexUtils.byteToHexStr(len) + HexUtils.Str2Hex(data);
           
            SignatureInfo SignatureInfo = SignHelper.getSignatureInfo(str);
            int pasmLen = SignatureInfo.Cadata.Length;
            string pasmData = SignatureInfo.Cadata;
            int signLen = SignatureInfo.Sighdata.Length;
            string signData = SignatureInfo.Sighdata;
            Console.WriteLine("pasmLen" + pasmLen);
            Console.WriteLine("pasmData" + pasmData);
            Console.WriteLine("signLen" + signLen);
            Console.WriteLine("signData" + signData);
            //int pasmLen = 354;
            byte[] pasmByte = HexUtils.intToBytes(pasmLen/2);
            for (int i = 0; i < pasmByte.Length; i++)
            {
                epc.Add(pasmByte[i]);
            }
            //byte[] pasmDataByte = HexUtils.strToHexByte("8A390D7C7E564BF84063B40064BCCB027C4FDFA2DDBC9168BF599ACE8D565FBB8D0EC6E4779670D8B46DE9F1F60897D738CA025D58F438C69B7487F8ACE08A5A57FD0B9D1CB00A73D564C866135D911898E0EFBA0E5DAEF28C3623BFF2A71520676E9DDC0E1E39F2D2BA42350EACF92A4DAD23435CD462E355ECE4FB4BB82CC2");
            byte[] pasmDataByte = HexUtils.strToHexByte(pasmData);
            for (int i = 0; i < pasmDataByte.Length; i++)
            {
                epc.Add(pasmDataByte[i]);
            }
            //byte[] signByte = HexUtils.intToBytes(128);
            byte[] signByte = HexUtils.intToBytes(signLen/2);
            for (int i = 0; i < signByte.Length; i++)
            {
                epc.Add(signByte[i]);
            }
            //byte[] signDataByte = HexUtils.strToHexByte("0000000531323130303030303032333030313132313030303030303333303031313231303030303030343330303131323130303030303035333030313132313030303030303633303031");
            byte[] signDataByte = HexUtils.strToHexByte(signData);
            for (int i = 0; i < signDataByte.Length; i++)
            {
                epc.Add(signDataByte[i]);
            }

            

            byte[] result = EpcScan.DataInstruct(processKey, epc);
          
            Console.WriteLine("resultComm" + HexUtils.byteToHexStr(result));
            return result;
        }
    }
}
